CREATE OR REPLACE function sst_quit(sqSstId IN NUMBER,sqLevel IN NUMBER,sqsQuit IN OUT VARCHAR2) return NUMBER AS
/** 
 * Creates or handles quit telegrams. 
 * @param sqSstId ID of telegram in SSTDAT to create or handle quit for
 * @param sqLevel Level of quit (1,2 for creating quit, 0 for handling quit,-1 if sending telegram has reached retry limit)
 * @param sqsQuit Contains created(level 1,2) or received(level 0) quit telegram
 * @return 0 if quit was created(quit will be sent) or quit was handled but is negative
 *        -1 if quit was not created(quit will not be sent) or not handled(resend after timeout expired)
 *        sqSstId if quit was handled positive, ID of telegram in SSTDAT to resend
 */
begin
  if(sqLevel in (1,2)) then
	  -- create quit telegram 
    sqsQuit := 'QUIT'||sqLevel;	
	  return 0;
	else
	  -- handle quit telegram
	  if(sqsQuit = 'QUIT2') then
		  update sstdat set sststat=2 where sstdat_id = sqSstId;
		  return sqSstId;
		else 
		  return -1;	
		end if;			
	end if;
end sst_quit;
/
